home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10991 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  103 lines

  1. Path: news.mindspring.com!usenet
  2. From: rudd@mindspring.com (Justin Rudd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Memory Leak?
  5. Date: Tue, 12 Mar 1996 05:14:49 GMT
  6. Organization: MindSpring Enterprises
  7. Message-ID: <4i31bk$s9c@B1FF.mindspring.com>
  8. References: <4i2tr9$q6k@newsgate.dircon.co.uk>
  9. NNTP-Posting-Host: rudd.mindspring.com
  10. X-Newsreader: Forte Free Agent v0.55
  11.  
  12. topher@dircon.co.uk (Chris Pyman) wrote:
  13.  
  14. >Hi all
  15.  
  16. >Does anybody know whether the following would lead to a memory leak?
  17. >(Assume this is a Win32 Console App written with Visual C++ v2.0)
  18.  
  19.  
  20. >main(int argc, char* argv[]) {
  21. >    CString s1("31-12-99");
  22. >    CString s2;
  23.  
  24. >    s2 = s1.Mid(0,2) + s1.Mid(3,2) + s1.Mid(6,2);
  25.  
  26. >    cout << s2;
  27. >    return 0;
  28. >}
  29.  
  30.  
  31. Nope...it sure won't.
  32.  
  33. >Obviously the idea is to set s2 to "311299", but I would have thought
  34. >that the Mid() method creates a new CString object and returns a
  35. >reference to it, in which case what becomes of the three "temporary"
  36. >objects used to build the string in s2?  Are they just floating around
  37. >with no way to get at them, or do they get properly deleted?  And if
  38. >the latter, how does MFC manage it?
  39.  
  40. The CString::Mid() does create a new CString but its scope is to that
  41. of the function.  So like any variable ( int, float, char, etc... ).
  42. When its scope is gone...it is gone.  I know this isn't the proper way
  43. to say it...but it explains it clearly enough.  
  44.  
  45. A better example is probably like this:
  46.  
  47. int Function()
  48. {
  49.     return 10;
  50. }
  51.  
  52. cout << Function();
  53.  
  54. This creates a temporary int that gets dumped to the screen.  The
  55. concept is the same with a CString.
  56.  
  57. As for how MFC manages it...I assume you mean how they manage the
  58. Mid() function and not the string inside.  But for the Mid() all they
  59. do is probably something like this:
  60.  
  61. CString CString::Mid( int first, int count )
  62. {
  63.     //not showing selection code because I don't want to get flamed for
  64.     //not being right :-)
  65.     //and the next part is TRULY speculation in my opinion
  66.  
  67.     return CString( /*whatever is holding selection here*/ );
  68.     //or return object if they created a temp CString to hold the     
  69.     //selection
  70. }
  71.  
  72. Like I said in the comments...this is just _MY OPINION_ if it doesn't
  73. agree with what anybody who is reading this post thinks sorry :-).
  74.  
  75. >The main reason I'm asking this is because I want to implement a
  76. >string class of my own, and would like to be able to use it to build
  77. >strings as simply as shown above, but I don't want loads of stray
  78. >objects floating around.  
  79.  
  80. Have fun...
  81.  
  82. [snip]
  83.  
  84. >Looking forward to hearing from you
  85. >Sorry if I'm being ignorant or dimwitted, but I've only read as much
  86. >of Stroustrup's book as my puny brain can take in at the moment.
  87.  
  88. Dude...don't be so worried about what people think...you'll get an
  89. ulcer ;-).  Besides I should be worried that someone won't agree with
  90. my post and I'll get e-mail bombed for being an ignorant MFC
  91. programmer.
  92.  
  93. >l+k
  94. >chris
  95.  
  96. BTW, all flames are welcome ;-)
  97.  
  98. Justin Rudd
  99. rudd@mindspring.com
  100.  
  101.  
  102.  
  103.